package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// DAO (Data Access Object 数据访问对象)
// 通过 BlogDao 的对象，完成针对数据库blog表的操作
public class BlogDao {

    // 1. 新增博客(用户提交博客时用到)
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 构造 sql
            String sql = "insert into blog values(null,?,?,now(),?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());
            // 执行 sql
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        } finally {
            // 关闭资源
            DBUtil.close(connection,statement,null);
        }
    }

    // 2. 根据 blogId 删除博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 构造 sql语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            // 执行 sql
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 关闭资源
            DBUtil.close(connection,statement,null);
        }
    }

    // 3. 查询博客列表 (博客列表页)
    // 把数据库里所有的博客都拿到.
    public List<Blog> getBlogs() {
        List<Blog> blogList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 构造sql
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            // 执行sql,拿到结果集合 并 进行遍历
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 此处读到的正文是整个文章内容, 博客列表页只希望展示一小部分，因此需要对文章内容进行简单的截断
                String content = resultSet.getString("content");
                if(content.length() > 100) {
                    content = content.substring(0,100);
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogList.add(blog);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            DBUtil.close(connection,statement,resultSet);
        }
        return blogList;
    }

    // 4. 根据 博客id 查询指定的某篇博客
    public Blog getBlog(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            // 根据 blogId 查询博客只可能有一个结果或没有结果
            if (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
}
